// pages/lifeService/repairReport/recordList.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "报事报修记录", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      goUrl: ''
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    tabList: [{
      id: '',
      name: '全部'
    }, {
      id: 1,
      name: '处理中'
    }, {
      id: 4,
      name: '待评价'
    }],
    list: [],
    curTabId: 0,
    userId: '',
    currentPage: 1,
    loading: false, // 是否正在加载中的标志
    hasMore: true, // 是否还有更多数据的标志
    totalPages: 0, //总页数
    orderStatus: '',
    shareTitle: '',
    shareImg: '',
    backHome:'',
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  goDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/lifeService/repairReport/recordDetail?id=' + id,
    })
  },
  //评价
  pj(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/lifeService/repairReport/evaluate?id=' + id,
    })
  },
  ckpj(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/lifeService/repairReport/evaluateDetail?id=' + id,
    })
  },
  chooseTab(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    that.setData({
      curTabId: id,
      orderStatus: id,
      list: [],
      currentPage: 1,
      hasMore: true,
      loading: false
    })
    that.loadData();
  },
  tj() {
    wx.navigateTo({
      url: '/pages/lifeService/repairReport/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //'oZYOb4ktAvmrb5TsIPBHlrGEKcZg';
    var openid = wx.getStorageSync('ly24_openid');
    if (options.backHome) {
      let defaultData = that.data.defaultData;
      defaultData.goUrl = '/pages/lifeService/managementFee/menu'
      that.setData({
        defaultData,
        backHome:true
      })
    }
    wx.request({
      url: that.data.baseUrl + 'api/getUserId.php',
      method: 'POST',
      data: {
        'openid': openid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          var userId = res.data.userId;
          that.setData({
            userId: userId
          });
          that.loadData("");
        }
      },
      fail: function (res) {}
    });
    //调用分享
    that.getShareInfo();
  },
  cancel(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let data = {
      userId: that.data.userId,
      id: id
    }
    wx.showLoading({
      title: '撤销中...'
    });
    wx.request({
      url: that.data.baseUrl + 'api/getApiData.php',
      method: 'POST',
      data: {
        'apiUrl': '/openapi/maintOrder/v1/cancel',
        'data': JSON.stringify(data)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          app.showTip('撤销成功！');
          that.setData({
            curTabId: that.data.curTabId,
            orderStatus: '',
            list: [],
            currentPage: 1,
            hasMore: true,
            loading: false
          })
          that.loadData();
        }else{
          app.showTip(res.data.msg);
        }
      },
      fail: function (res) {
        //调用最终上传数据
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
      }
    });
  },
  loadData() {
    var that = this;
    if (that.data.loading) return; // 如果当前正在加载中，则直接返回
    that.setData({
      loading: true
    }); // 设置正在加载中的标志
    var userId = that.data.userId;
    var currentPage = that.data.currentPage;
    var data = {
      userId: userId,
      currentPage: currentPage,
      numPerPage: 10,
      orderStatus: that.data.orderStatus
    }
    wx.showLoading({
      title: '数据加载中...'
    });
    wx.request({
      url: that.data.baseUrl + 'api/getApiData.php',
      method: 'POST',
      data: {
        'apiUrl': '/openapi/maintOrder/v1/page',
        'data': JSON.stringify(data)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.hideLoading();
          let oldList = that.data.list;
          let newList = oldList.concat(res.data.data.resultData);
          that.setData({
            list: newList,
            totalPages: res.data.data.totalPages,
            loading: false
          })
          if (that.data.list.length == res.data.data.totalCount) {
            that.setData({
              hasMore: false
            });
          }
        }
      },
      fail: function (res) {
        //调用最终上传数据
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
      }
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.hasMore) return; // 如果没有更多数据，则直接返回
    this.setData({
      currentPage: this.data.currentPage + 1
    })
    this.loadData(); // 继续加载数据
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})